﻿//配置插件目录
layui.config({
    base: '/mods/'
});
layui.use(['form', 'laydate', 'util', 'jquery', 'layer', 'table'], function () {
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var util = layui.util;
    var tid = null;
    var fid = null;
    var $ = layui.jquery;
    $(function () {
        clickTree();
    });
    //$(function () {
    //    TreeList();
    //});
    $(function () {
        SelTime();
    });
    $(function () {
        SelLevel();
    });
    // 提交事件
    form.on('submit(demo1)', function (data) {
        var obj = {};
        var field = data.field;
        obj.PId = field.TypeInfo;
        obj.TypeId = field.TypeId;
        obj.TypeName = field.TypeName;
        obj.EventDesc = field.EventDesc;
        obj.EventImage = field.Image;
        obj.EventLevel = field.EventLevel;
        obj.EventTimeLimit = field.EventTimeLimit;
        $.ajax({
            url: "http://8.140.245.229:8000/webroute/api/Event/HandleAddEvent",
            type: "post",
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            data: JSON.stringify(obj),
            contentType: "application/json",
            success: res => {
                if (res > 0) {
                    layer.confirm('添加成功', function (index) {
                        ShowInfo(1);
                        layer.close(index);
                        layer.close(res);
                    });

                } else {
                    layer.alert("添加失败");
                }
            }
        })
        return false; // 阻止默认 form 跳转
    });
    function ShowInfo() {
        $.ajax({
            url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleShowAdmin",
            type: 'get',
            dataType: 'json',
            data: {
                TypeId: $("#DocumentNumber").val(),
                TypeName: $("#DocumentName").val()
            },
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            success: function (res) {
                table.render({
                    elem: '#test',
                    cols: [[
                        { type: 'checkbox', align: 'center' },
                        { field: 'TypeId', title: '类型编号', align: 'center' },
                        { field: 'PId', title: '父类型', align: 'center' },
                        { field: 'TypeName', title: '类型名称', align: 'center' },
                        { title: '操作', width: 150, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true }
                    ]],

                    data: res
                })

            }
        })
    }
    function checkTree() {
        $("#TypeInfo").val(tid);
        layui.form.render();
    }
    function clickTree() {
        $.ajax({
            url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleEventSelect?PId=" + tid,
            async: false,
            type: "get",
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            success: function (res) {
                var select = $("#TypeInfo");
                $.each(res, function (index, item) {
                    select.append($('<option>').val(item.EventTypeName).text(item.EventTypeName));

                })
                layui.form.render('select');
                //设置对应 id 的节点选中
            }
        })
    }
    function SelLevel() {
        $.ajax({
            url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleLevel",
            async: false,
            type: "get",
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            success: function (res) {
                var select = $("#EventLevel");
                $.each(res, function (index, item) {
                    select.append($('<option>').val(item.LevelName).text(item.LevelName))
                })
                layui.form.render('select');
            }
        })
    }
    function SelTime() {
        $.ajax({
            url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleTimeLimit",
            async: false,
            type: "get",
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            success: function (res) {
                var select = $("#EventTimeLimit");
                $.each(res, function (index, item) {
                    select.append($('<option>').val(item.LimitName).text(item.LimitName))
                })
                layui.form.render('select');
            }
        })
    }
});
layui.use(['table', 'dropdown', 'upload', 'jquery'], function () {
    var upload = layui.upload;
    var table = layui.table;
    var dropdown = layui.dropdown;
    var laypage = layui.laypage;
    var $ = layui.jquery;
    var laydate = layui.laydate;
    var layer = layui.layer; // 添加layui弹出层模块
    upload.render({
        elem: '#ID-upload-demo-btn',
        headers: {
            'Authorization': "Bearer " + localStorage["token"] + ""
        },
        url: 'http://8.140.245.229:8000/webroute/api/Event/UpFile', // 实际使用时改成您自己的上传接口即可。
        done: function (res) {

            $('#Image').attr('src', 'http://8.140.245.229:8000/webroute/File/' + res);
            console.log($('#Image'));
        },
    });
    // 创建渲染实例
});
function RestEmployee() {
    $(".layui-form").reset();
    layui.form.render();
}